package cn.kgc.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtil {
    private JsonUtil(){

    }

    //json转对象:反序列化
    public static <T> T JsonToObject(String json,Class<T> class1){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(json,class1);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //对象转json:序列化
    public static String ObjectToJson(Object obj){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
